package Review;

public class Test26 {

	public static void main(String[] args) {
	      ComplexListNode node1=new ComplexListNode(1);   
	      ComplexListNode node2=new ComplexListNode(2);
	      ComplexListNode node3=new ComplexListNode(3);   
	      ComplexListNode node4=new ComplexListNode(4);
	      ComplexListNode node5=new ComplexListNode(5);   
	      ComplexListNode node6=new ComplexListNode(6);
	      node1.Sibling = node1;
	      node1.next=node2;
	      node2.Sibling = node6;
	      node2.next=node3;
	      node3.next=node4;
	      node4.next=node5;
	      node5.next=node6;
	      node6.next=null;
	      
//		  ComplexListNode newhead=cloneList(node1);
		  ComplexListNode newhead=ComplexListCopy.cloneList(node1);
		  ComplexListNode p = newhead;
		  while(p!=null)
		  {
			  System.out.print(p.value);
			  System.out.print(" ");
			  p=p.next;
		  }
	}
    public static ComplexListNode cloneList(ComplexListNode head){
    	if(head==null){
    		return null;
    	}
    	cloneNode(head);
    	setSibling(head);
    	return getNewList(head);
    	
    }
    public static void cloneNode(ComplexListNode head){
    	ComplexListNode p = head;
    	while(p!=null){
    		ComplexListNode clone = new ComplexListNode(p.value);
    		clone.next=p.next;
    		p.next=clone;
    		p=clone.next;
    	}
    }
    public static void setSibling(ComplexListNode head){
    	ComplexListNode p = head;
    	while(p!=null){
    		ComplexListNode pclone = p.next;
    		if(p.Sibling!=null){
    			pclone.Sibling = p.Sibling.next;
    		}
    		p=pclone.next;
    	}
    	
    }
    public static ComplexListNode getNewList(ComplexListNode head){
    	ComplexListNode newHead = head.next;
    	ComplexListNode p = head;
    	p.next=newHead.next;
    	p=p.next;
    	ComplexListNode clone = newHead;
    	while(p!=null){
    		clone.next=p.next;
    		p.next=clone.next.next;
    		clone=clone.next;
    		p=p.next;
    	}		
    	return newHead;
    }
}

class ComplexListCopy{
	public static ComplexListNode cloneList(ComplexListNode head){
    	if(head==null){
    		return null;
    	}
    	cloneNode(head);
    	copySibling(head);
    	return copyNext(head);
    	
    }
	public static void cloneNode(ComplexListNode node){
		if(node == null){
			return ;
		}
		while(node != null){
			ComplexListNode newNode = new ComplexListNode(node.value);
			newNode.next = node.next;
			node.next = newNode;
			node = newNode.next;
		}
	}
	public static void copySibling(ComplexListNode node){
		ComplexListNode cloneNode;
		while(node!=null){
			cloneNode = node.next;
			if(node.Sibling != null){
				cloneNode.Sibling = node.Sibling.next;
			}
			node = cloneNode.next;
		}
	}
	public static ComplexListNode copyNext(ComplexListNode node){
		ComplexListNode cloneNode;
		ComplexListNode head = null;
		if(node != null){
			head = node.next;
		}
		while(node!=null){
			cloneNode = node.next;
			node.next = cloneNode.next;
			node = node.next;
			if(node != null){
				cloneNode.next = node.next;
			}
		}
		return head;
	}
}
